﻿package  {
	
	import flash.display.MovieClip;
	import flash.events.*;
	
	public class wasys extends MovieClip {
		
		var pe:Number=0;
		var pec:Number=0;
		var allcell:Array=new Array();
		var tallingcell:displaycell=null;
		var clearstpoint:vector2=null;
		var holdline:line=null;
		public var controlteam:Number=0;
		public function wasys() 
		{
			// constructor code
			root.loaderInfo.addEventListener(Event.COMPLETE,conffrr);
			//this.addEventListener(Event.FRAME_CONSTRUCTED,conffrr);
			this.addEventListener(MouseEvent.MOUSE_DOWN,mfd);
			this.addEventListener(MouseEvent.MOUSE_UP,mff);
			this.addEventListener(Event.ENTER_FRAME,entf);
		}
		private function initgui(e:Number) 
		{ 

				if(e==1)
	    		{
					
					cell1.sysinit(this);
					cell2.sysinit(this);
					cell3.sysinit(this);
					cell4.sysinit(this);
					cell5.sysinit(this);
					cell6.sysinit(this);
					allcell.push(cell1);
					allcell.push(cell2);
					allcell.push(cell3);
					allcell.push(cell4);
					allcell.push(cell5);
					allcell.push(cell6);
					cell2.setteam(1);
					cell3.setteam(2);
					cell5.setteam(1);
					cell6.setteam(2);
					this.addChild(new fpser());
					//trace("ch");
				}

		}
		private function conffrr(e:Event):void
		{ 
			//trace("pe:",pe,this.currentFrame);
			var bbo:Boolean;
			var selid:Number=pec++;
				try 
				{
					
					if(pe!=this.currentFrame)
					{
					//trace("pe:",pe,this.currentFrame,selid);
					
					initgui(this.currentFrame);
					pe=this.currentFrame;
					//trace("pe:",pe,this.currentFrame,selid);
					trace(allcell.length);
					}
				}
				catch (err:Error) 
				{ 
				trace("err:",err.message);
				//this.removeEventListener(Event.FRAME_CONSTRUCTED,conffrr);
				}
				finally
				{
					//trace("final",selid);
				}
				
		}
		public function hittest(xx:Number,yy:Number)
		{
			var i:uint;
			var tmparr:Array=new Array();
			for(i=0;i<allcell.length;i++)
			{
				if(allcell[i].hitTestPoint(xx,yy,true))
				{
					tmparr.push(allcell[i]);
				}
			}
			return tmparr;
		}
		public function mfd(a:MouseEvent)//mouseudown event
		{
			trace("md",tallingcell);
			if(tallingcell==null && holdline==null)
			{
				clearstpoint=new vector2(stage.mouseX,stage.mouseY);
				holdline=new line();
				holdline.width=1;
				holdline.x=clearstpoint.x;
				holdline.y=clearstpoint.y;
				this.addChild(holdline);
			}
		}
		public function mff(a:MouseEvent)//mouseup event
		{
			trace("mf",tallingcell);
			var i:uint;
			if(tallingcell!=null)
			{
				tallingcell.arrowupevent();
				tallingcell=null;
			}
			else if(clearstpoint!=null)
			{
				
				
				for(i=0;i<allcell.length;i++)
				{
					allcell[i].cuttentacle(clearstpoint,new vector2(stage.mouseX,stage.mouseY));
				}
				this.removeChild(holdline);
					holdline=null;
					clearstpoint=null;
			}
			
		}
		public function entf(a:Event)//mouseudown event
		{
			if(holdline!=null)
			{
				//trace("et",tallingcell);
				if(tallingcell!=null)
				{
					this.removeChild(holdline);
					holdline=null;
					clearstpoint=null;
				}
				else
				{
					var tmpvs:vector2=new vector2(stage.mouseX,stage.mouseY);
					tmpvs=tmpvs.sub(clearstpoint);
					holdline.rotation=0;
					holdline.width=tmpvs.length();
					
					holdline.rotation=tmpvs.getdegree();
				}
			}
		}
		public function settallingcell(e:displaycell)
		{
			tallingcell=e;
		}
	}
}
